package kosta.controller;

import java.util.HashMap;
import java.util.List;


import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kosta.model.vo.ListVO;


import kosta.model.vo.NoticeVO;
import kosta.model.vo.SearchVO;
import kosta.service.NoticeService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/notice")
public class NoticeController {
	
	@Resource(name="noticeServiceImpl")
	private NoticeService noticeService;

    //@PathVariable 클라이언트 요청 path를 변수로 할당받을 때 사용
    @RequestMapping("/{viewId}View.do")
    public String showView(@PathVariable("viewId") String viewId){
            System.out.println(viewId);
            return viewId+"View.notice";
    }
    
	
	//공지사항 글쓰기
	@RequestMapping("/admin_writeNotice.do")	
	public ModelAndView writeNotice(NoticeVO nvo) {	
		noticeService.writeNotice(nvo);
		System.out.println("(controller)공지사항 글쓰기"+nvo);
		return new ModelAndView("redirect:/notice/getNoticeBoardList.do");
	}
	//공지사항 삭제
	@RequestMapping("/admin_deleteNotice.do")
	public String deleteNotice(HttpServletRequest request) {
		String noticeNo = request.getParameter("noticeNo");
		noticeService.deleteNotice(noticeNo);
		System.out.println("(controller)공지사항 삭제"+noticeNo);
		return "redirect:getNoticeBoardList.do";
	}
	
	
	//공지사항 수정
	@RequestMapping("/admin_updateNotice.do")
	public String updateNotice(HttpServletRequest request,NoticeVO nvo) {
		noticeService.updateNotice(nvo);
		System.out.println("(Controller)컨트롤러 수정"+nvo);
		return "redirect:updateNoticeResult.do?noticeNo="+nvo.getNoticeNo();
	}
	@RequestMapping("/admin_updateNoticeResult.do")
	public ModelAndView updateNoticeResult(HttpServletRequest request){
		String noticeNo = request.getParameter("noticeNo");
		NoticeVO nvo = noticeService.showNoticeContentNotHit(noticeNo);
		return new ModelAndView("noticeUpdateResult.notice","nvo",nvo);
	}
	
	@RequestMapping("/admin_updateNoticeForm.do")
	public ModelAndView updateNoticeForm(HttpServletRequest request){
		String noticeNo = request.getParameter("noticeNo");
		NoticeVO nvo = noticeService.showNoticeContentNotHit(noticeNo);
		System.out.println("(Controller)컨트롤러 수정"+noticeNo+nvo);
		return new ModelAndView("noticeUpdateForm.notice","nvo",nvo);
	}
	
	//index에서 이동하는 페이지. 최근 공지사항 5개와 최근 등록된 경매품 5개 보여줌.
	@RequestMapping("/home.do")
	public ModelAndView home(){
		HashMap<String, List> mainInfoMap=noticeService.getMainInfoList();			
		
		return new ModelAndView("projectHome.notice","main",mainInfoMap);		
	}
	
	//공지사항 전체 리스트를 불러오는 메소드
	@RequestMapping("/getNoticeBoardList.do")
	public ModelAndView getNoticeBoardList(HttpServletRequest request, SearchVO svo){
		ListVO lvo = noticeService.getNoticeBoardList(svo);
		return new ModelAndView("noticeBoardList.notice","lvo",lvo);		
	}
	
	//공지사항 상세보기
		@RequestMapping("/showNoticeContent.do")
		public ModelAndView showNoticeContent(HttpServletRequest request,
                HttpServletResponse response){			
			String noticeNo = request.getParameter("noticeNo");			
			NoticeVO nvo=null;			
			Cookie[] cookies=request.getCookies();		
			if(cookies==null || cookies.length==0){
				response.addCookie(new Cookie("myboard","|"+noticeNo+"|"));
				//조회수 증가 
				//boardService.getPostingByNo(no);	
				nvo = noticeService.showNoticeContent(noticeNo);
			}else{
				Cookie myboardCookie=null;
				for(int i=0;i<cookies.length;i++){
					if(cookies[i].getName().equals("myboard")){
						myboardCookie=cookies[i];
						break;
					}
				}
				if(myboardCookie==null){
					response.addCookie(new Cookie("myboard","|"+noticeNo+"|"));
					//조회수 증가 
					//vo=boardService.getPostingByNo(no);	
					nvo = noticeService.showNoticeContent(noticeNo);
				}else{
					String value=myboardCookie.getValue();
					if(value.indexOf("|"+noticeNo+"|")!=-1){
						//조회수 증가 X
						//vo=boardService.getPostingByNoNotHit(no);
						nvo = noticeService.showNoticeContentNotHit(noticeNo);
						
						
					}else{// -1 즉 존재하지 않으면 
						value+="|"+noticeNo+"|";
						response.addCookie(new Cookie("myboard",value));
						//조회수 증가 
						//vo=boardService.getPostingByNo(no);	
						nvo = noticeService.showNoticeContent(noticeNo);
					}
				}
			}			
		return new ModelAndView("showNoticeContent.notice","nvo",nvo);		
	}
}
